package info.pello.struts2;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;


public class StockAction extends ActionSupport implements ModelDriven<Stock> {

	private static final long serialVersionUID = -6659354552584240539L;

	
	private Stock stock = new Stock();
	private List<Stock> listaStock = new ArrayList<Stock>();
	private StockDAO stockDAO = new StockDAO();

	
	@Override
	public Stock getModel() {
		return stock;
	}
	
	/**
	 * To save or update user.
	 * @return String
	 */
	public String guardarOActualizar()
	{	
		stockDAO.guardarOActualizarStock(stock);
		return SUCCESS;
	}
	
	/**
	 * To list all users.
	 * @return String
	 */
	public String listar()
	{
		listaStock = stockDAO.listarStock();
		return SUCCESS;
	}
	
	/**
	 * To delete a user.
	 * @return String
	 */
	public String eliminar()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		stockDAO.eliminarStock(Long.parseLong(request.getParameter("id")));
		return SUCCESS;
	}
	
	/**
	 * To list a single user by Id.
	 * @return String
	 */
	public String editar()
	{
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		stock = stockDAO.listarStockPorId(Long.parseLong(request.getParameter("id")));
		return SUCCESS;
	}
	
	public Stock getStock() {
		return stock;
	}

	public void setEquipo(Stock stock) {
		this.stock = stock;
	}

	public List<Stock> getListaStock() {
		return listaStock;
	}

	public void setListaStock(List<Stock> listaStock) {
		this.listaStock = listaStock;
	}
	


}
